﻿<!---
 
  Copyright (c) 2010, Cristian Costantini - cristian@millemultimedia.it
  
  NotifyManager is licensed under the terms of the Open Source GPL 3.0 license. 
	
	http://www.gnu.org/licenses/gpl.html
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--->

<cfcomponent output="false">

	<cffunction name="init" access="public" returntype="XMLLoader">
		
		<cfreturn this/>
	</cffunction>
	
	<cffunction name="load" access="public" returntype="void" output="false">
		<cfargument name="framework" type="notifyManager.NotifyManager" required="true" />
		<cfargument name="xmlConfig" type="Xml" required="true" />
		
    	<cfscript>
    	variables.framework = arguments.framework;
    	variables.xmlConfig = arguments.xmlConfig;
    	
    	loadServices();
		
		loadNotices();
		
    	loadEvents();
    	</cfscript>
    </cffunction>
	
	<!--- event --->
	
	<cffunction name="loadEvents" access="private" returntype="void" output="false">
    	<cfscript>
		var eventsXml = xmlSearch( variables.xmlConfig, '/notifyManager/events/event/' );
		var broadcastsXml = "";
		var broadcasts = "";
		var event = "";
		var notifier = new notifyManager.event.Notifier();
		var i = 0;
		var b = 0;
		var e = 0;
		
		for( i=1; i<=arrayLen(eventsXml); i++ ){
		
			notifier.configureEvent( eventsXml[ i ].XmlAttributes[ 'name' ] );
			
			broadcasts = new notifyManager.event.Broadcasts();
			
			broadcastsXml = xmlSearch( eventsXml[ i ], 'broadcasts/' );
			
			for( b=1; b<=arrayLen( broadcastsXml[ 1 ].XmlChildren ); b++ ){
			
				event = xmlSearch( broadcastsXml[ 1 ].XmlChildren[ b ], 'notice/' );
				
				for( e=1; e<=arrayLen( event ); e++ ){
			
					broadcasts.registerListener( broadcastsXml[ 1 ].XmlChildren[ b ].XmlName, variables.framework.getNotice( event[ i ].XmlAttributes[ 'name' ] ), false );
			
				}
			
			}
			
			notifier.registerListener( eventsXml[ i ].XmlAttributes[ 'name' ], broadcasts, false );
		
		}
		
		variables.framework.setNotifier( notifier );
    	</cfscript>
    </cffunction>
	
	<!--- notice --->
	
	<cffunction name="loadNotices" access="private" returntype="void" output="false">
    	<cfscript>
		var noticesXml = xmlSearch( variables.xmlConfig, '/notifyManager/notices/notice/' );
		var propertyXml = "";
		var property = {};
		var notice = "";
		var i = 0;
		var p = 0;
		
		for( i=1; i<=arrayLen(noticesXml); i++ ){
		
			propertyXml = xmlSearch( noticesXml[i], 'property/' );
			property = {};
			
			for( p=1; p<=arrayLen( propertyXml ); p++ ){
			
				property.put( propertyXml[p].XmlAttributes['key'], propertyXml[p].XmlChildren[1].XmlText );
				
			}
		
			notice = noticeFactory( variables.framework.getService( noticesXml[ i ].XmlAttributes[ 'services' ] ), property );
		
			notice.setName( noticesXml[ i ].XmlAttributes[ 'name' ] );
			notice.setType( noticesXml[ i ].XmlAttributes[ 'type' ] );
			notice.setTemplate( noticesXml[ i ].XmlAttributes[ 'template' ] );
			
			variables.framework.addNotice( noticesXml[ i ].XmlAttributes[ 'name' ], notice );
		
		}
		
    	</cfscript>
    </cffunction>
	
	<cffunction name="noticeFactory" access="private" returntype="notifyManager.metadata.Notice" output="false">
		<cfargument name="service" type="Component" required="true" />
		<cfargument name="property" type="Struct" required="true" />
		
		<cfreturn createObject( 'component', 'notifyManager.metadata.Notice' ).init( arguments.service, arguments.property ) />
    </cffunction>
	
	<!--- services --->
	
	<cffunction name="loadServices" access="private" returntype="void" output="false">
    	<cfscript>
    	var servicesXml = xmlSearch( variables.xmlConfig, '/notifyManager/services/service/' );
		var propertyXml = "";
		var services = {};
		var property = {};
		var i = 0;
		var p = 0;
		
		for( i=1; i<=arrayLen( servicesXml ); i++ ){
		
			propertyXml = xmlSearch( servicesXml[i], 'property/' );
			property = structNew();
			
			for( p=1; p<=arrayLen( propertyXml ); p++ ){
			
				property.put( propertyXml[p].XmlAttributes['key'], propertyXml[p].XmlChildren[1].XmlText );
				
			}
			
			variables.framework.addService( servicesXml[i].XmlAttributes['name'], serviceFactory( servicesXml[i].XmlAttributes['class'], property ) );
		}
    	</cfscript>
    </cffunction>
	
	<cffunction name="serviceFactory" access="private" returntype="notifyManager.services.IServiceAdapter" output="false">
    	<cfargument name="class" type="string" required="true" />
		<cfargument name="args" type="struct" required="false" default="#structNew()#" />
		
		<cfreturn createObject( 'component', #arguments.class# ).init( arguments.args ) />
    </cffunction>

</cfcomponent>